package com.hql.handicraft_display_system.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hql.handicraft_display_system.admin.entity.Crafts;
import com.hql.handicraft_display_system.admin.entity.vo.CraftsVO;
import com.hql.handicraft_display_system.admin.mapper.CraftsMapper;
import com.hql.handicraft_display_system.admin.result.PageModel;
import com.hql.handicraft_display_system.admin.service.CraftsService;
import com.hql.handicraft_display_system.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hql
 * @since 2022-01-08
 */
@Service
public class CraftsServiceImpl extends ServiceImpl<CraftsMapper, Crafts> implements CraftsService {

    @Override
    public PageModel<CraftsVO> listByPage(Integer page, Integer limit, String keyword) {
        LambdaQueryWrapper<Crafts> wrapper = Wrappers.lambdaQuery(Crafts.class);
        // 判断是否存在查询条件
        if (!ObjectUtils.isEmpty(keyword)) {
            wrapper.like(Crafts::getName, keyword);
        }
        // 分页查询
        Page<Crafts> craftsPage = new Page<>(page, limit);
        List<Crafts> craftsList = baseMapper.selectPage(craftsPage, wrapper).getRecords();
        List<CraftsVO> craftsVOList = craftsList.stream().map(crafts -> {
            CraftsVO craftsVO = new CraftsVO();
            BeanUtils.copyProperties(crafts, craftsVO);
            // TODO 暂时设置一下所属展览
            craftsVO.setExhibitionName("北京xxxx展览");
            LocalDateTime exhibitionTime = crafts.getExhibitionTime();
            craftsVO.setExhibitionTime(DateUtils.dateToString(exhibitionTime));
            return craftsVO;
        }).collect(Collectors.toList());
        // 封装最终返回结果
        PageModel<CraftsVO> pageModel = new PageModel<>();
        pageModel.setList(craftsVOList);
        pageModel.setTotal(craftsPage.getTotal());
        return pageModel;
    }

    @Override
    public void changeStatus(Integer id, Integer status) {
        // 查询当前id的工艺品信息
        Crafts crafts = baseMapper.selectById(id);
        // 设置状态
        crafts.setStatus(status);
        // 更新
        baseMapper.updateById(crafts);
    }

    @Override
    public void addOrUpdate(Crafts crafts) {
        // 查询当前工艺品是否已经存在
        Crafts tempCrafts = baseMapper.selectOne(
                Wrappers.lambdaQuery(Crafts.class)
                        .eq(Crafts::getName, crafts.getName()));
        if (tempCrafts == null) {
            //  保存
            baseMapper.insert(crafts);
        } else {
            // 更新
            crafts.setId(tempCrafts.getId());
            baseMapper.updateById(crafts);
        }
    }
}
